s_ch = []
for i in range (2,10000):
    a = 1
    for l in range(2, i//2):
        if i % l == 0:
            a = 0
            break
    if a:
        s_ch.append(i)
itog = 0
ch = input()
if not (999 < int(ch) < 10000):
    ch = '0000'
for i0 in '' , ch[0]:
    for i1 in '' , ch[1]:
        for i2 in '' , ch[2]:
            for i3 in '' , ch[3]:
                if i0+i1+i2+i3:
                    j = int(i0+i1+i2+i3)
                    if j in s_ch:
                        itog = max(itog, j)
print(itog)
                
    
